#include <QLabel>
#include <QHBoxLayout>
#include <QDebug>
#include <QtGui>
#include <QSplitter>
#include "pagehumiture.h"
#include "myButton.h"
#include <QDateTime>
#include <QMessageBox>
#include <cmath>
#include <QFontDatabase>
#include <math.h>
#include <QSettings>

PAGEHUMITURE::PAGEHUMITURE(QWidget *parent) :
    QWidget(parent)
{
    //Timecount=1;
    flag=1;
    flag1=1;
    flag2=1;
    isInTesting = false;
    stop_pressed = false;

    dataBc=new QLabel(tr(""));//保存
    dataJc=new QLabel(tr(""));
    dataBfb=new QLabel(tr(""));


   // debug_press=false;

//设置字体样式
    QString fontName= "";

    int loadedFontID = QFontDatabase::addApplicationFont(":/image/res/_H_Helvetica.ttc");
    QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID);
    qDebug()<<loadedFontFamilies;
    if(!loadedFontFamilies.empty())
        fontName = loadedFontFamilies.at(6);
    QFont ft(fontName);

    ft.setPixelSize(20);

    Vlabel=new QLabel(tr("额定二次电压:"));
    Vlabel->setFont(ft);
    Vlabel->setStyleSheet("color:blue;");
   // Vlabel->setFixedSize(50,50);

    ft.setPixelSize(20);
    //ft.setBold(true);
    jclabel=new QLabel(tr("角差:"));
    jclabel->setFont(ft);
    jclabel->setStyleSheet("color:blue;");
    //jclabel->setFixedSize(50,30);

    bclabel=new QLabel(tr("比差:"));
    bclabel->setFont(ft);
    bclabel->setStyleSheet("color:blue;");

    ft.setPixelSize(20);
    //ft.setBold(false);
    bfblabel=new QLabel(tr("待设定百分表:"));
    bfblabel->setFont(ft);
    bfblabel->setStyleSheet("color:blue;");
   // bfblabel->setFixedSize(50,30);

    danwei1=new QComboBox();
    danwei1->setFixedSize(70,40);
    danwei1->addItem("V");
    danwei1->addItem("/√3V");
    danwei1->setStyleSheet("color:red;");
    danwei1->setFont(ft);

    danwei2=new QLabel(tr("%"));
    danwei2->setFont(ft);
    danwei3=new QLabel(tr("%"));
    danwei3->setFont(ft);
    danwei4=new QLabel(tr("%"));
    danwei4->setFont(ft);
    danwei5=new QLabel(tr("%"));
    danwei5->setFont(ft);

    fen=new QLabel(tr("分"));
    hu=new QLabel(tr("弧"));
    //ft.setPixelSize(20);
    fen->setFont(ft);
    hu->setFont(ft);

    radio1=new QRadioButton;
    radio1->setFixedSize(20,20);
    radio1->setChecked(true);
    radio2=new QRadioButton;
    radio2->setFixedSize(20,20);


    sinlabel1=new QPushButton(tr("极坐标系"));
    sinlabel2=new QPushButton(tr("直角坐标系"));
    sinlabel1->setFont(ft);
    sinlabel1->setStyleSheet("color:blue;border:0px;");
    sinlabel2->setFont(ft);
    sinlabel2->setStyleSheet("color:blue;border:0px;");
    sinstate1=new QRadioButton;
    sinstate1->setFixedSize(20,20);
    sinstate1->setChecked(true);
    sinstate2=new QRadioButton;
    sinstate2->setFixedSize(20,20);
    connect(sinlabel1,SIGNAL(clicked()),this,SLOT(state_on1()));
    connect(sinlabel2,SIGNAL(clicked()),this,SLOT(state_on2()));


    ft.setPixelSize(20);
    ft.setBold(true);
    bcEdit=new QLineEdit("");
    bcEdit->setFixedSize(200,40);
    bcEdit->setFont(ft);
    bcEdit->setStyleSheet("color:red;");
    bcEdit->installEventFilter(this);

    jcEdit=new QLineEdit("");
    jcEdit->setFixedSize(200,40);
    jcEdit->setFont(ft);
    jcEdit->setStyleSheet("color:red;");
    jcEdit->installEventFilter(this);
    ft.setBold(false);

    bfbEdit=new QLineEdit("");
    bfbEdit->setFixedSize(150,40);
    bfbEdit->setFont(ft);
    bfbEdit->setStyleSheet("color:red;");
    bfbEdit->installEventFilter(this);

    vEdit=new QLineEdit("");
    vEdit->setFixedSize(150,40);
    vEdit->setFont(ft);
    vEdit->setStyleSheet("color:red;");
    vEdit->installEventFilter(this);



    bcBtn1=new QPushButton;
    bcBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    bcBtn1->setFixedSize(60,40);

    bcBtn2=new QPushButton;
    bcBtn2->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    bcBtn2->setFixedSize(60,40);
    bcBtn3=new QPushButton;
    bcBtn3->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    bcBtn3->setFixedSize(60,40);
    bcBtn4=new QPushButton;
    bcBtn4->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    bcBtn4->setFixedSize(60,40);
    bcBtn5=new QPushButton;
    bcBtn5->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    bcBtn5->setFixedSize(60,40);
    bcBtn6=new QPushButton;
    bcBtn6->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    bcBtn6->setFixedSize(60,40);

    jcBtn1=new QPushButton;
    jcBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    jcBtn1->setFixedSize(60,40);
    jcBtn2=new QPushButton;
    jcBtn2->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    jcBtn2->setFixedSize(60,40);
    jcBtn3=new QPushButton;
    jcBtn3->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    jcBtn3->setFixedSize(60,40);
    jcBtn4=new QPushButton;
    jcBtn4->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    jcBtn4->setFixedSize(60,40);
    jcBtn5=new QPushButton;
    jcBtn5->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    jcBtn5->setFixedSize(60,40);
    jcBtn6=new QPushButton;
    jcBtn6->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    jcBtn6->setFixedSize(60,40);

    bfbBtn1=new QPushButton;
    bfbBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    bfbBtn1->setFixedSize(60,40);

    bfbBtn2=new QPushButton;
    bfbBtn2->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    bfbBtn2->setFixedSize(60,40);

    VBtn1=new QPushButton;
    VBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
    VBtn1->setFixedSize(60,40);
    VBtn2=new QPushButton;
    VBtn2->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    VBtn2->setFixedSize(60,40);
    ft.setBold(true);
    cu1=new QPushButton(tr("粗"));
    cu1->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:24px;color:#C4C4C4;background-color:#666;border-radius:15px;}");
    cu1->setFixedSize(50,40);
    cu2=new QPushButton(tr("粗"));
    cu2->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:24px;color:#C4C4C4;background-color:#666;border-radius:15px;}");
    cu2->setFixedSize(50,40);

    cu3=new QPushButton(tr("粗"));
    cu3->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:24px;color:#C4C4C4;background-color:#666;border-radius:15px;}");
    cu3->setFixedSize(50,40);

    add1=new QPushButton(tr("±"));
    add1->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:24px;color:#C4C4C4;background-color:#666;border-radius:15px;}");
    add1->setFixedSize(50,40);
    add2=new QPushButton(tr("±"));
    add2->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:24px;color:#C4C4C4;background-color:#666;border-radius:15px;}");
    add2->setFixedSize(50,40);
    ft.setBold(false);



    ft.setPixelSize(20);
    showbfblabel=new QLabel(tr("工作百分表:"));
    showbfblabel->setFont(ft);
    showbfblabel->setStyleSheet("color:blue;");
    ft.setBold(true);
    ft.setPixelSize(24);
    start = new myButton;
    start->setText("开始");
    start->setFont(ft);
    start->setFixedSize(250,50);

    start1=new myButton;
    start1->setText("开始");
    start1->setFont(ft);
    start1->setFixedSize(150,50);

    stop = new myButton;
    stop->setText("停止");
    stop->setFont(ft);
    stop->setFixedSize(250,50);
    ft.setBold(false);
//bcBtn布局
    QVBoxLayout *layout1=new QVBoxLayout;
    layout1->addWidget(bcBtn1);
    layout1->addWidget(bcBtn4);
    QVBoxLayout *layout11=new QVBoxLayout;
    layout11->addWidget(bcBtn2);
    layout11->addWidget(bcBtn5);
    QVBoxLayout *layout111=new QVBoxLayout;
    layout111->addWidget(bcBtn3);
    layout111->addWidget(bcBtn6);
    QVBoxLayout *layout1111=new QVBoxLayout;
    layout1111->addWidget(cu1);
    layout1111->addWidget(add1);
//JcBtn布局
    QVBoxLayout *layout2=new QVBoxLayout;
    layout2->addWidget(jcBtn1);
    layout2->addWidget(jcBtn4);
    QVBoxLayout *layout22=new QVBoxLayout;
    layout22->addWidget(jcBtn2);
    layout22->addWidget(jcBtn5);
    QVBoxLayout *layout222=new QVBoxLayout;
    layout222->addWidget(jcBtn3);
    layout222->addWidget(jcBtn6);
    QVBoxLayout *layout2222=new QVBoxLayout;
    layout2222->addWidget(cu2);
    layout2222->addWidget(add2);
//jcEdit布局
    QHBoxLayout *layout3=new QHBoxLayout;
    layout3->addWidget(jclabel);
    layout3->addSpacing(5);
    layout3->addWidget(jcEdit);
    QHBoxLayout *layout33=new QHBoxLayout;
    layout33->addStretch(1);
    layout33->addWidget(fen);

    QHBoxLayout *layout333=new QHBoxLayout;
    layout333->addSpacing(20);
    layout333->addLayout(layout3);
    layout333->addLayout(layout33);
    layout333->addStretch(1);
//bcEdit布局
    QHBoxLayout *layout4=new QHBoxLayout;
    layout4->addSpacing(20);
    layout4->addWidget(bclabel);
    layout4->addSpacing(5);
    layout4->addWidget(bcEdit);
    layout4->addWidget(danwei4);
    layout4->addStretch(1);
//vEdit布局
    QHBoxLayout *layout5=new QHBoxLayout;
    layout5->addSpacing(20);
    layout5->addWidget(Vlabel);
    layout5->addSpacing(5);
    layout5->addWidget(vEdit);
    layout5->addWidget(danwei1);
    layout5->addStretch(1);
//vBtn布局
//    QVBoxLayout *layout55=new QVBoxLayout;
//    layout55->addWidget(VBtn1);
//    layout55->addWidget(VBtn2);
//    layout55->addStretch(1);
//bfbEdit布局
    QHBoxLayout *layout6=new QHBoxLayout;
    layout6->addSpacing(20);
    layout6->addWidget(bfblabel);
    layout6->addWidget(bfbEdit);
    layout6->addWidget(danwei2);
    layout6->addStretch(1);
//bfbBtn布局
    QHBoxLayout *layout66=new QHBoxLayout;
    layout66->addWidget(bfbBtn1);
    layout66->addWidget(cu3);
    QVBoxLayout *layout666=new QVBoxLayout;
    layout666->addLayout(layout66);
    layout666->addWidget(bfbBtn2);
//start布局
    QHBoxLayout *layout7=new QHBoxLayout;
    //layout7->addSpacing(120);
    layout7->addWidget(showbfblabel);
    layout7->addSpacing(10);
    layout7->addWidget(bfbEdit);
    layout7->addWidget(danwei5);
    layout7->addStretch(1);
    QHBoxLayout *layout7777=new QHBoxLayout;
    layout7777->addStretch(1);
    layout7777->addLayout(layout5);
    layout7777->addSpacing(1);
    layout7777->addLayout(layout7);
    layout7777->addLayout(layout666);
    layout7777->addStretch(1);
    //layout7777->addLayout(layout777);



//坐标系
    QHBoxLayout *layout8=new QHBoxLayout;
    //layout8->addStretch(1);
    layout8->addWidget(sinstate1);
    layout8->addWidget(sinlabel1);
    layout8->addStretch(1);
    QHBoxLayout *layout88=new QHBoxLayout;
    //layout88->addStretch(1);
    layout88->addWidget(sinstate2);
    layout88->addWidget(sinlabel2);
    layout88->addStretch(1);

    QVBoxLayout *layout888=new QVBoxLayout;
    layout888->addLayout(layout8);
    layout888->addLayout(layout88);

//阴影1
    QWidget *widgetTest = new QWidget;
    widgetTest->setFixedSize(850,100);
    widgetTest->setStyleSheet(".QWidget{background-color:#A9A9A9;border:1px solid #bbc;border-radius:5px;}");
    //QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    //shadowEffect->setBlurRadius(5);
    //shadowEffect->setXOffset(5);
    //shadowEffect->setYOffset(5);
    //widgetTest->setGraphicsEffect(shadowEffect);
    widgetTest->setLayout(layout7777);

    QHBoxLayout *mainlayout1=new QHBoxLayout;
    mainlayout1->addStretch(1);
    mainlayout1->addWidget(widgetTest);
    mainlayout1->addStretch(1);

    QHBoxLayout *layout9=new QHBoxLayout;
    layout9->addSpacing(40);
    layout9->addLayout(layout1);
    layout9->addLayout(layout11);
    layout9->addLayout(layout111);
    layout9->addLayout(layout1111);
    layout9->addStretch(1);

    QHBoxLayout *layout10=new QHBoxLayout;
    layout10->addSpacing(30);
    layout10->addLayout(layout2);
    layout10->addLayout(layout22);
    layout10->addLayout(layout222);
    layout10->addLayout(layout2222);
    layout10->addStretch(1);

//阴影3

    QWidget *widgetTest3 = new QWidget;
    widgetTest3->setFixedSize(350,100);
    widgetTest3->setStyleSheet(".QWidget{background-color:#bbb;border:1px solid #bbc;border-radius:5px;}");
    widgetTest3->setLayout(layout9);
    QWidget *widgetTest4 = new QWidget;
    widgetTest4->setFixedSize(350,100);
    widgetTest4->setStyleSheet(".QWidget{background-color:#bbb;border:1px solid #bbc;border-radius:5px;}");
    widgetTest4->setLayout(layout10);


    QWidget *widgetTest2 = new QWidget;
    widgetTest2->setFixedSize(350,60);
    widgetTest2->setStyleSheet(".QWidget{border:0px solid #bbc;border-radius:5px;}");
    widgetTest2->setLayout(layout4);

    QWidget *widgetTest22 = new QWidget;
    widgetTest22->setFixedSize(350,60);
    widgetTest22->setStyleSheet(".QWidget{border:0px solid #bbc;border-radius:5px;}");
    widgetTest22->setLayout(layout333);



    QVBoxLayout *LAY1=new QVBoxLayout;
    LAY1->addStretch(1);
    LAY1->addWidget(widgetTest2);
    LAY1->addSpacing(5);
    LAY1->addWidget(widgetTest3);
    LAY1->addStretch(1);

    QHBoxLayout *layout_LAY1=new QHBoxLayout;
    layout_LAY1->addStretch(1);
    layout_LAY1->addLayout(LAY1);
    layout_LAY1->addStretch(1);

    QVBoxLayout *LAY2=new QVBoxLayout;
    LAY2->addStretch(1);
    LAY2->addWidget(widgetTest22);
    LAY2->addSpacing(5);
    LAY2->addWidget(widgetTest4);
    LAY2->addStretch(1);

    QHBoxLayout *layout_LAY2=new QHBoxLayout;
    layout_LAY2->addStretch(1);
    layout_LAY2->addLayout(LAY2);
    layout_LAY2->addStretch(1);

    QWidget *widgetTest5 = new QWidget;
    widgetTest5->setFixedSize(410,213);
    widgetTest5->setStyleSheet(".QWidget{background-color:#A9A9A9;border:1px solid #bbc;border-radius:5px;}");
    widgetTest5->setLayout(layout_LAY1);

    QWidget *widgetTest7 = new QWidget;
    widgetTest7->setFixedSize(410,213);
    widgetTest7->setStyleSheet(".QWidget{background-color:#A9A9A9;border:1px solid #bbc;border-radius:5px;}");
    widgetTest7->setLayout(layout_LAY2);


    QHBoxLayout *layout16=new QHBoxLayout;
    layout16->addStretch(1);
    layout16->addWidget(widgetTest5);
    layout16->setSpacing(26);
    layout16->addWidget(widgetTest7);
    layout16->addStretch(1);

    QHBoxLayout *layout15=new QHBoxLayout;
    layout15->addStretch(1);
    layout15->addWidget(start);
    layout15->addWidget(start1);
    layout15->addSpacing(20);
    layout15->addLayout(layout888);
    layout15->addSpacing(20);
    layout15->addWidget(stop);
    layout15->addStretch(1);
    start1->hide();
    QWidget *widgetTest6 = new QWidget;
    widgetTest6->setFixedSize(850,70);
    widgetTest6->setStyleSheet(".QWidget{background-color:#A9A9A9;border:1px solid #bbc;border-radius:5px;}");
    widgetTest6->setLayout(layout15);

    QHBoxLayout *layout17=new QHBoxLayout;
    layout17->addStretch(1);
    layout17->addWidget(widgetTest6);
    layout17->addStretch(1);

    QVBoxLayout *mainlayout=new QVBoxLayout(this);
    mainlayout->addLayout(mainlayout1);
    mainlayout->addSpacing(15);
    mainlayout->addLayout(layout16);
    mainlayout->addSpacing(15);
    mainlayout->addLayout(layout17);
    mainlayout->addStretch(1);

    setLayout(mainlayout);

    theKeyBoard = new Dialog(this);

    connect(cu1,SIGNAL(clicked()),this,SLOT(on_cu1_clicked()));
    connect(cu2,SIGNAL(clicked()),this,SLOT(on_cu2_clicked()));
    connect(cu3,SIGNAL(clicked()),this,SLOT(on_cu3_clicked()));
    connect(bcBtn1,SIGNAL(clicked()),this,SLOT(on_bcBtn1_clicked()));
    connect(bcBtn2,SIGNAL(clicked()),this,SLOT(on_bcBtn2_clicked()));
    connect(bcBtn3,SIGNAL(clicked()),this,SLOT(on_bcBtn3_clicked()));
    connect(bcBtn4,SIGNAL(clicked()),this,SLOT(on_bcBtn4_clicked()));
    connect(bcBtn5,SIGNAL(clicked()),this,SLOT(on_bcBtn5_clicked()));
    connect(bcBtn6,SIGNAL(clicked()),this,SLOT(on_bcBtn6_clicked()));

    connect(jcBtn1,SIGNAL(clicked()),this,SLOT(on_jcBtn1_clicked()));
    connect(jcBtn2,SIGNAL(clicked()),this,SLOT(on_jcBtn2_clicked()));
    connect(jcBtn3,SIGNAL(clicked()),this,SLOT(on_jcBtn3_clicked()));
    connect(jcBtn4,SIGNAL(clicked()),this,SLOT(on_jcBtn4_clicked()));
    connect(jcBtn5,SIGNAL(clicked()),this,SLOT(on_jcBtn5_clicked()));
    connect(jcBtn6,SIGNAL(clicked()),this,SLOT(on_jcBtn6_clicked()));

    connect(VBtn1,SIGNAL(clicked()),this,SLOT(on_VBtn1_clicked()));
    connect(VBtn2,SIGNAL(clicked()),this,SLOT(on_VBtn2_clicked()));
    connect(bfbBtn1,SIGNAL(clicked()),this,SLOT(on_bfbBtn1_clicked()));
    connect(bfbBtn2,SIGNAL(clicked()),this,SLOT(on_bfbBtn2_clicked()));

    connect(add1,SIGNAL(clicked()),this,SLOT(on_AddBtn1_clicked()));
    connect(add2,SIGNAL(clicked()),this,SLOT(on_AddBtn2_clicked()));

    connect(start,SIGNAL(clicked(QString)),this,SLOT(on_start1_chicked()));
    //connect(start1,SIGNAL(clicked(QString)),this,SLOT(on_start1_chicked()));
    connect(stop,SIGNAL(clicked(QString)),this,SLOT(on_stop_chicked()));


    connect(bcEdit,SIGNAL(textChanged(QString)),this,SLOT(superior_limit()));
    connect(jcEdit,SIGNAL(textChanged(QString)),this,SLOT(superior_limit()));
    connect(bfbEdit,SIGNAL(textChanged(QString)),this,SLOT(superior_limit()));


    labtime = new QTimer;
    connect(labtime,SIGNAL(timeout()),this,SLOT(labhide()));
    labtime1 = new QTimer;
    connect(labtime1,SIGNAL(timeout()),this,SLOT(labhide1()));
    labtime2 = new QTimer;
    connect(labtime2,SIGNAL(timeout()),this,SLOT(labhide2()));

    limitlabel=new QLabel(this);
    limitlabel->setText(tr("高/低于比差范围.."));
    limitlabel->setFont(ft);
    limitlabel->setStyleSheet("background-color:color(200,200,200,150);border-radius:5px;");
    limitlabel->hide();

    limitlabel1=new QLabel(this);
    limitlabel1->setText(tr("高/低于角差范围.."));
    limitlabel1->setFont(ft);
    limitlabel1->setStyleSheet("background-color:color(200,200,200,150);border-radius:5px;");
    limitlabel1->hide();

    limitlabel2=new QLabel(this);
    limitlabel2->setText(tr("高于百分表范围.."));
    limitlabel2->setFont(ft);
    limitlabel2->setStyleSheet("background-color:color(200,200,200,150);border-radius:5px;");
    limitlabel2->hide();

    widgetInfo = new QWidget(this);
    //widgetInfo->setStyleSheet(".QWidget{background-color:rgb(204,102,0);border-radius:15px;}");
    //    widgetInfo->setStyleSheet(".QWidget{background-color:#666;border-radius:15px;}");
    widgetInfo->setStyleSheet(".QWidget{background-color:qColor(250,250,0,150);border-radius:15px;}");
    labInfo = new QLabel("欢迎使用...");
    labInfo->setStyleSheet("color:yellow;font:22px;font-weight:bold;");
    labInfo->setFixedWidth(400);
    labInfo->setAlignment(Qt::AlignCenter);

    QVBoxLayout *layoutInfo = new QVBoxLayout;
    layoutInfo->setAlignment(Qt::AlignCenter);
    layoutInfo->addWidget(labInfo);

    widgetInfo->setLayout(layoutInfo);
    widgetInfo->setFixedSize(450,80);
    widgetInfo->move(280,80);

    QTimer::singleShot(1000, this, SLOT(widgetInfoHide()));

    timer_stop = new QTimer;

    connect(timer_stop,SIGNAL(timeout()),this,SLOT(try_stop_testing()));

   // debug->move(0,0);
    LoadConfig();

    dataBc->setText(bcEdit->text());
    dataJc->setText(jcEdit->text());
    dataBfb->setText(bfbEdit->text());

    labReternedBfb = new QLabel(this);
    setDataStyle(labReternedBfb);
    labReternedBfb->move(608,1);
    labReternedBc = new QLabel(this);
    setDataStyle(labReternedBc);
    labReternedBc->move(220,120);
    labReternedJc = new QLabel(this);
    setDataStyle(labReternedJc);
    labReternedJc->move(650,120);

    labReternedBfb->hide();
    labReternedBc->hide();
    labReternedJc->hide();



}

void PAGEHUMITURE::try_stop_testing()
{
    static int try_times = 0;

    if(isInTesting == true)
    {
        sendBuf.resize(0);
        sendBuf[0] = 0x3d;

        qDebug()<<"bfb Edite stop:"<<sendBuf.toHex();

        emit cmdSend();
        sendBuf.clear();

        if( ++try_times == 10)
        {
            QMessageBox::warning(this,tr("错误"),QString("底层未响应停止指令"),QMessageBox::tr("确定"));

            timer_stop->stop();

            stop_pressed = true;
            isInTesting = false;

            start->setEnabled(true);
            start->highLight(true);

            try_times = 0;
        }
    }
    else
    {
        timer_stop->stop();

        try_times = 0;
        LoadConfig();
    }
}

void PAGEHUMITURE::setDataStyle(QLabel *lab)
{
    QFont ft;
    ft.setPixelSize(20);
    lab->setFixedSize(150,40);
    lab->setFont(ft);
    lab->setAlignment(Qt::AlignCenter);
    lab->setLineWidth(1);
    lab->setFrameStyle(QFrame::Box|QFrame::Raised);
    lab->setStyleSheet("background-color:#ddd;color:darkgreen;");
}

void PAGEHUMITURE::recvResult(QByteArray rcvdByte)
{
    if(!isInTesting) return;

    //qDebug()<<"recvResult::::"<<rcvdByte.toHex();

    if((unsigned char)rcvdByte[0] == 0xff)
    {
        QMessageBox::warning(this,tr("错误"),QString("无法链接到硬件："+rcvdByte.mid(1)),QMessageBox::tr("确定"));
        isInTesting = false;

        widgetInfo->hide();

        start->setEnabled(true);
        start->highLight(true);

        return;
    }
    readData(rcvdByte);
}

bool PAGEHUMITURE::eventFilter(QObject *watched, QEvent *e)
{
    if(watched == bcEdit)
    {
        if(e->type() == QEvent::FocusIn)
        {
            //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");

            style = "bcEdit";

            if(isInTesting == true)
            {
                timer_stop->start(500);
            }

            widgetInfo->hide();

            theKeyBoard->setCurrentActive(bcEdit);
            qDebug()<<"bcedit: keyBoard show()";
            //superior_limit();
        }
        else if(e->type() == QEvent::FocusOut)
        {
//            style = "";
            theKeyBoard->hide();
        }
    }

    if(watched == jcEdit)
    {
        if(e->type() == QEvent::FocusIn)
        {
            //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");

            if(isInTesting == true)
            {
                timer_stop->start(500);
            }

            widgetInfo->hide();

            style = "jcEdit";
            theKeyBoard->setCurrentActive(jcEdit);
            qDebug()<<"jcedit: keyBoard show()";
        }
        else if(e->type() == QEvent::FocusOut)
        {
//            style = "";
            theKeyBoard->hide();
        }
    }

    if(watched ==vEdit)
    {
        if(e->type()==QEvent::FocusIn)
        {
            if(isInTesting == true)
            {
                timer_stop->start(500);
            }

            widgetInfo->hide();

            theKeyBoard->setCurrentActive(vEdit);
            qDebug()<<"vEdit: keyBoard show()";
        }
        else if(e->type()==QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }

    if(watched==bfbEdit)
    {
        if(e->type()==QEvent::FocusIn)
        {
            style = "bfbEdit";
           // bfbEdit->setStyleSheet("background-color:#FFFFFF;color:black;");

            if(isInTesting == true)
            {
                timer_stop->start(500);
            }

            widgetInfo->hide();

            theKeyBoard->setCurrentActive(bfbEdit);
            qDebug()<<"bfbEdit: keyBoard show()";
//            start->setEnabled(true);
//            start->highLight(true);
        }
        else if(e->type()==QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    return QWidget::eventFilter(watched, e);
}


void PAGEHUMITURE::on_cu1_clicked()
{
    if(cu1->text()=="粗")
    {
        cu1->setText("细");
        flag=0;
    }
    else
    {
        cu1->setText("粗");
        flag=1;
    }
    changeButton();
}
void PAGEHUMITURE::on_cu2_clicked()
{
    if(cu2->text()=="粗")
    {
        cu2->setText("细");
        flag1=0;
    }
    else
    {
        cu2->setText("粗");
        flag1=1;
    }
    changeButton1();
}

void PAGEHUMITURE::on_cu3_clicked()
{
    if(cu3->text()=="粗")
    {
        cu3->setText("细");
        flag2=0;
    }
    else
    {
        cu3->setText("粗");
        flag2=1;
    }
    changeButton2();
}
/*//////////////////////////////////////////////////////////////////////////////////////////////////////*/

void PAGEHUMITURE::on_bcBtn1_clicked()
{
    voice(1);
    style = "bcEdit";
    //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bcEdit->text().toDouble();
    double bcText=dataBc->text().toDouble();

    double cuAdjust=1,xiAdjust=0.1;
    if(cu1->text()=="粗")
    {
        bcText+=cuAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }
    else
    {
        bcText+=xiAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }

    if( stop_pressed != true && isInTesting == true)
    {
        //isInTesting = false;
        sendData();
    }
    // superior_limit();
    qDebug()<<bcText;

}

void PAGEHUMITURE::on_bcBtn2_clicked()
{
    voice(1);
    style = "bcEdit";
    //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bcEdit->text().toDouble();
    double bcText=dataBc->text().toDouble();
    double cuAdjust=0.1,xiAdjust=0.01;
    if(cu1->text()=="粗")
    {
        bcText+=cuAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }
    else
    {
        bcText+=xiAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

    qDebug()<<bcText;
}

void PAGEHUMITURE::on_bcBtn3_clicked()
{
    voice(1);
    style = "bcEdit";
    //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bcEdit->text().toDouble();
    double bcText=dataBc->text().toDouble();
    double cuAdjust=0.01,xiAdjust=0.001;
    if(cu1->text()=="粗")
    {
        bcText+=cuAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }
    else
    {
        bcText+=xiAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}

void PAGEHUMITURE::on_bcBtn4_clicked()
{
    voice(1);
    style = "bcEdit";
    //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bcEdit->text().toDouble();
    double bcText=dataBc->text().toDouble();
    double cuAdjust=-1,xiAdjust=-0.1;
    if(cu1->text()=="粗")
    {
        bcText+=cuAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }
    else
    {
        bcText+=xiAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}

void PAGEHUMITURE::on_bcBtn5_clicked()
{
    voice(1);
    style = "bcEdit";
    //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bcEdit->text().toDouble();
    double bcText=dataBc->text().toDouble();
    double cuAdjust=-0.1,xiAdjust=-0.01;
    if(cu1->text()=="粗")
    {
        bcText+=cuAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }
    else
    {
        bcText+=xiAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}


void PAGEHUMITURE::on_bcBtn6_clicked()
{
    voice(1);
    style = "bcEdit";
    //bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bcEdit->text().toDouble();
    double bcText=dataBc->text().toDouble();
    double cuAdjust=-0.01,xiAdjust=-0.001;
    if(cu1->text()=="粗")
    {
        bcText+=cuAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }
    else
    {
        bcText+=xiAdjust;
        bcEdit->setText(QString::number(bcText));
        dataBc->setText(QString::number(bcText));
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}


void PAGEHUMITURE::on_jcBtn1_clicked()
{
    voice(1);
    style = "jcEdit";
    //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    qDebug()<<"fuxuankuang:"<<radio1->isChecked();
    qDebug()<<"fuxuankuang:"<<radio2->isChecked();
    //double bcText=jcEdit->text().toDouble();
    double bcText=dataJc->text().toDouble();
    if(radio1->isChecked())
    {
        double cuAdjust=5,xiAdjust=0.5;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    else if(radio2->isChecked())
    {
        double cuAdjust=1,xiAdjust=0.1;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}

void PAGEHUMITURE::on_jcBtn2_clicked()
{
    voice(1);
    style = "jcEdit";
    //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=jcEdit->text().toDouble();
    double bcText=dataJc->text().toDouble();
    if(radio1->isChecked())
    {
        double cuAdjust=0.5,xiAdjust=0.05;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    else if(radio2->isChecked())
    {
        double cuAdjust=0.1,xiAdjust=0.01;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}

void PAGEHUMITURE::on_jcBtn3_clicked()
{
    voice(1);
    style = "jcEdit";
    //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=jcEdit->text().toDouble();
    double bcText=dataJc->text().toDouble();
    if(radio1->isChecked())
    {
        double cuAdjust=0.05,xiAdjust=0.005;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    else if(radio2->isChecked())
    {
        double cuAdjust=0.01,xiAdjust=0.001;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}


void PAGEHUMITURE::on_jcBtn4_clicked()
{
    voice(1);
    style = "jcEdit";
    //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=jcEdit->text().toDouble();
    double bcText=dataJc->text().toDouble();
    if(radio1->isChecked())
    {
        double cuAdjust=-5,xiAdjust=-0.5;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    else if(radio2->isChecked())
    {
        double cuAdjust=-1,xiAdjust=-0.1;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    //}
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }


}


void PAGEHUMITURE::on_jcBtn5_clicked()
{
    voice(1);
    style = "jcEdit";
    //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=jcEdit->text().toDouble();
    double bcText=dataJc->text().toDouble();
    if(radio1->isChecked())
    {
        double cuAdjust=-0.5,xiAdjust=-0.05;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    else if(radio2->isChecked())
    {
        double cuAdjust=-0.1,xiAdjust=-0.01;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    //}

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }


}


void PAGEHUMITURE::on_jcBtn6_clicked()
{
    voice(1);
    style = "jcEdit";
    //jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=jcEdit->text().toDouble();
    double bcText=dataJc->text().toDouble();
    if(radio1->isChecked())
    {
        double cuAdjust=-0.05,xiAdjust=-0.005;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    else if(radio2->isChecked())
    {
        double cuAdjust=-0.01,xiAdjust=-0.001;
        if(cu2->text()=="粗")
        {
            bcText+=cuAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
        else
        {
            bcText+=xiAdjust;
            jcEdit->setText(QString::number(bcText));
            dataJc->setText(QString::number(bcText));
        }
    }
    // }

    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }

}
void PAGEHUMITURE::on_VBtn1_clicked()
{
    voice(1);
    double bcText=vEdit->text().toDouble();
    double Adjust=0.01;
    bcText+=Adjust;
    vEdit->setText(QString::number(bcText));
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}
void PAGEHUMITURE::on_VBtn2_clicked()
{
    voice(1);
    double bcText=vEdit->text().toDouble();
    double Adjust=-0.01;
    bcText+=Adjust;
    vEdit->setText(QString::number(bcText));
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}
void PAGEHUMITURE::on_bfbBtn1_clicked()
{
    voice(1);
    style = "bfbEdit";
    //bfbEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bfbEdit->text().toDouble();
    double bcText=dataBfb->text().toDouble();
    double cuAdjust=5,xiAdjust=0.1;
    if(cu3->text()=="粗")
    {
        bcText=(cuAdjust+100)/100*bcText;
       // bcText+=cuAdjust;
        bfbEdit->setText(QString::number(bcText));
        dataBfb->setText(QString::number(bcText));
    }
    else
    {
        //bcText+=xiAdjust;
        bcText=(100+xiAdjust)/100*bcText;
        bfbEdit->setText(QString::number(bcText));
        dataBfb->setText(QString::number(bcText));
    }
    qDebug()<<bcText;
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}

void PAGEHUMITURE::on_bfbBtn2_clicked()
{
    voice(1);
    style = "bfbEdit";
    //bfbEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
    //double bcText=bfbEdit->text().toDouble();
    double bcText=dataBfb->text().toDouble();
    double cuAdjust=-5,xiAdjust=-0.1;
    if(cu3->text()=="粗")
    {
        //bcText+=cuAdjust;
        bcText=(cuAdjust+100)/100*bcText;
        bfbEdit->setText(QString::number(bcText));
        dataBfb->setText(QString::number(bcText));
    }
    else
    {
        //bcText+=xiAdjust;
        bcText=(100+xiAdjust)/100*bcText;
        bfbEdit->setText(QString::number(bcText));
        dataBfb->setText(QString::number(bcText));
    }
    qDebug()<<bcText;
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}

void PAGEHUMITURE::on_AddBtn1_clicked()
{
    voice(1);
    double bcText=dataBc->text().toDouble();
    bcText=-bcText;
    bcEdit->setText(QString::number(bcText));
    dataBc->setText(QString::number(bcText));
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}
void PAGEHUMITURE::on_AddBtn2_clicked()
{
    voice(1);
    double bcText=dataJc->text().toDouble();
    bcText=-bcText;
    jcEdit->setText(QString::number(bcText));
    dataJc->setText(QString::number(bcText));
    if( stop_pressed != true && isInTesting == true)
    {
//        isInTesting = false;
        sendData();
    }
}

void PAGEHUMITURE::superior_limit()
{
    if(style == "bcEdit")
    {
        qDebug()<<"现在比差值为："<<bcEdit->text().toFloat();
        if( bcEdit->text().toFloat() > 12.0 )
        {        //bcBtn1->setEnabled(false);
            widgetInfo->hide();
            limitlabel->move(193,163);
            limitlabel->setFixedSize(198,37);
            limitlabel->show();
            labtime->start(1000);
            dataBc->setText(QString::number(12));
            bcEdit->setText(QString::number(12));
        }
        else if( bcEdit->text().toFloat() < -12.0 )
        {
            widgetInfo->hide();
            limitlabel->move(193,163);
            limitlabel->setFixedSize(198,37);
            limitlabel->show();
            labtime->start(1000);
            dataBc->setText(QString::number(-12));
            bcEdit->setText(QString::number(-12));
        }
    }
//    else if(style == "jcEdit")
//    {
//        if( jcEdit->text().toFloat() > 600.0 )
//        {
//            widgetInfo->hide();
//            limitlabel1->setFixedSize(198,37);
//            limitlabel1->move(629,163);
//            limitlabel1->show();
//            labtime1->start(1000);
//            jcEdit->setText(QString::number(600));
//            dataJc->setText(QString::number(600));

//        }
//        else if ( jcEdit->text().toFloat() < -600.0 )
//        {
//            widgetInfo->hide();
//            limitlabel1->setFixedSize(198,37);
//            limitlabel1->move(629,163);
//            limitlabel1->show();
//            labtime1->start(1000);
//            jcEdit->setText(QString::number(-600));
//            dataJc->setText(QString::number(-600));
//        }
//    }
    else if(style == "bfbEdit")
    {
        //qDebug()<<"bfb的值"<<bfbEdit->text().toFloat();
        if( bfbEdit->text().toFloat() > 150.0 )
        {
            widgetInfo->hide();
            limitlabel2->setFixedSize(198,37);
            limitlabel2->move(570,90);
            limitlabel2->show();
            labtime2->start(1000);
            bfbEdit->setText(QString::number(150));
            dataBfb->setText(QString::number(150));
            qDebug()<<"bfb:"<<dataBfb->text().toFloat();
        }
    }

}

void PAGEHUMITURE::changeButton()
{
    voice(1);
    if(flag==1)
    {
        qDebug()<<"改变button:"<<flag;
        bcBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        bcBtn2->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        bcBtn3->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        bcBtn4->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
        bcBtn5->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
        bcBtn6->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");

    }
    else
    {
        qDebug()<<"改变button:"<<flag;
        bcBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        bcBtn2->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        bcBtn3->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        bcBtn4->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
        bcBtn5->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
        bcBtn6->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
    }
}

void PAGEHUMITURE::changeButton1()
{
    voice(1);
    if(flag1==1)
    {
        qDebug()<<"改变button1:"<<flag1;
        jcBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        jcBtn2->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        jcBtn3->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        jcBtn4->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
        jcBtn5->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
        jcBtn6->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");

    }
    else
    {
        qDebug()<<"改变button1:"<<flag1;
        jcBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        jcBtn2->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        jcBtn3->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        jcBtn4->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
        jcBtn5->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
        jcBtn6->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
    }
}

void PAGEHUMITURE::changeButton2()
{
    voice(1);
    if(flag2==1)
    {
        bfbBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up.png)  no-repeat center;border:0px;");
        bfbBtn2->setStyleSheet("background:url(:/image/res/arrowheads_down.png)  no-repeat center;border:0px;");
    }
    else
    {
        bfbBtn1->setStyleSheet("background:url(:/image/res/arrowheads_up_light.png)  no-repeat center;border:0px;");
        bfbBtn2->setStyleSheet("background:url(:/image/res/arrowheads_down_light.png)  no-repeat center;border:0px;");
    }
}

void PAGEHUMITURE::sendData()
{
    labInfo->setText("正在调整输出，请稍候...");
    widgetInfo->show();

//    bcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
//    jcEdit->setStyleSheet("background-color:#FFFFFF;color:black;");
//    bfbEdit->setStyleSheet("background-color:#FFFFFF;color:black;");

    QString bcData = bcEdit->text();
    QString jcData = jcEdit->text();
    QString bfbData = bfbEdit->text();
    QString vData = vEdit->text();

    //dataJc->setText(QString::number(jcData.toFloat(),'f'));
//    dataBc->setText(bcData);
//    dataJc->setText(jcData);
//    dataBfb->setText(bfbData);

    qDebug()<<"data1Bc:::"<<dataBc->text();
    getdata.append(0x40);
    getdata.append(Analytical_data(bcData,9,1));

 //   if (radio1->isChecked())
//    {
    getdata.append(Analytical_data(jcData,9,2));
//    }
//    else if (radio2->isChecked())
//    {
//        qDebug()<<"Before jc hu"<<jcData;
//        double aa = jcData.toDouble()*3.6*30/3.1415926535898;
//        getdata.append(Analytical_data(QString::number(aa, 'f', 6),9,1));
//        //qDebug()<<"After jc hu"<<aa;
//        qDebug()<<"After jc hu1"<<QString::number(aa, 'f', 6);
//    }

    getdata.append(Analytical_data(bfbData,6,3));

    getdata.append(Analytical_data(vData,6,4));
    sendBuf.append(getdata);
    if(sinstate1->isChecked())
    {
        sendBuf.append(01);
    }
    else
    {
        sendBuf.append(02);
    }
    sendBuf.append(0x23);

    emit cmdSend();

    qDebug()<<"发送数据"<<sendBuf.toHex();
    //qDebug()<<"bc发送数据"<<bcEdit->text();
    getdata.clear();
    sendBuf.clear();
}


void PAGEHUMITURE::on_start_chicked()
{
    voice(1);

    if(bcEdit->text() == ""
            || vEdit->text() == ""
            || jcEdit->text() == ""
            || bfbEdit->text() == "")    //判断文本框是否为空
    {
        QMessageBox::warning(this,tr("错误"),tr("设定参数不能为空!"),QMessageBox::tr("确定"));
        return;
    }

    else
    {
        stop_pressed = false;
        isInTesting = true;//表示

        start->setEnabled(false);
        start->highLight(false);

        sendData();
    }
    SaveConfig();
}

void PAGEHUMITURE::on_start1_chicked()//内检开始
{
    voice(1);
    if(bcEdit->text() == ""
            || vEdit->text() == ""
            || jcEdit->text() == ""
            || bfbEdit->text() == "")    //判断文本框是否为空
    {
        QMessageBox::warning(this,tr("错误"),tr("设定参数不能为空!"),QMessageBox::tr("确定"));
        return;
    }
    else
    {
        dataBc->setText(bcEdit->text());
        dataJc->setText(jcEdit->text());
        dataBfb->setText(bfbEdit->text());

        labInfo->setText("正在自检,请稍候...");
        widgetInfo->show();
        QByteArray srr;
        srr.resize(3);
        srr[0]=0x40;
        srr[1]=0x21; //内检标志
        srr[2]=0x23;
        qDebug()<<srr.toHex()<<"stop clicked";
        sendBuf.append(srr);
        emit cmdSend();
        sendBuf.clear();
        srr.clear();
        isInTesting = true;
        stop_pressed = false;
        start->setEnabled(false);
        start->highLight(false);
    }
    SaveConfig();
}

void PAGEHUMITURE::on_stop_chicked()
{
    voice(1);

    if(isInTesting == true) //开始已点击
    {
        timer_stop->start(500);
    }

    widgetInfo->hide();

}

QByteArray PAGEHUMITURE::Analytical_data(QString s,int len,int flag)
{
    int data = 0;
    QByteArray bytesSend = "";
    QString strTmp = "";

    if(flag == 1) // for bc 小数位6位
    {
        data = s.toFloat() * 1000000;
    }
    else if(flag == 2) //for jc 小数位3位
    {
        data = s.toFloat() * 1000;
    }
    else if(flag == 3) //for bfb 小数位3位
    {
        data = s.toFloat() * 1000;
    }
    else //for v 小数位3位
    {
        if(danwei1->currentText() == "V")
        {
           data = s.toFloat() * 10000;
        }
        else if(danwei1->currentText() == "/√3V")
        {
            data = (s.toFloat()/1.732050807)* 10000;
        }
        qDebug()<<"The volate is"<<data/10000;

    }

    strTmp = QString::number(data);

    bytesSend.resize(0);

    int i = 0;

    if(flag == 1 || flag == 2) //bc,jc带符号位
    {
        i++;

        if(data > 0) //正数
        {
            bytesSend[0] = 0x30;
        }
        else
        {
            bytesSend[0] = 0x31;
            strTmp = strTmp.mid(1);//mid掉符号位，剩下数据位
        }
    }

    int indexStart = len - strTmp.length();//规定长度-实际长度；

    for(; i < len; i++)
    {
        if(i < indexStart) //添加0
        {
            bytesSend.append(0x30);
        }
        else
        {
            bytesSend.append(strTmp.data()[i - indexStart]);//将strTmp数据的剩余数据添加到补完位0x00的bytesSend里
        }
    }

    qDebug()<<"Analytical_data: flag, bytesSend"<<flag<<bytesSend.toHex()<<strTmp;

    return bytesSend;
}

void PAGEHUMITURE::readData(QByteArray redata)
{

    QByteArray show_bfb = "";
    QByteArray send_bfb = "";
    QByteArray array="";
    QString show_bc,show_jc;

    static QByteArray bfb_array ;
    static QByteArray bc_array ;
    static QByteArray jc_array ;

    QByteArray m;
    QList <QByteArray> strlist;
    int i = 0;

    float bfb = 0.0;

    send_bfb.append(redata);

    if( 0x40 == redata[0]) //判断回传的数据redata[0]是否是0x40开始标志
    {
        redata = redata.mid(2); //去掉开始位和长度位

        if( 0x2A == redata[0]) //测试完成标识
        {
            voice(0);
            qDebug()<<"测试完成....";
            labInfo->setText("完成");
            widgetInfo->show();
            QTimer::singleShot(500, this, SLOT(widgetInfoHide())); //虚框隐藏

            //bcEdit->setStyleSheet("background-color:#ddd;color:red;font-size:20px");
            //jcEdit->setStyleSheet("background-color:#ddd;color:red;font-size:20px");
            //bfbEdit->setStyleSheet("background-color:#ddd;color:red;font-size:20px");

            start->setEnabled(true);
            start->highLight(true);

        }
        else if (0x21 == redata[0]) //确认收到自检命令再发测试数据
        {
//            Send_stop->start(100);
            qDebug()<<"定时0.2秒";
            QTime t;
            t.start();
            while(t.elapsed()<200) QCoreApplication::processEvents();

            widgetInfo->hide();
            sendData();
        }
        else if (0x25 == redata[0]) //自检完成，开始测试
        {
            qDebug()<<"测试完成....";
            //labInfo->setText("自检完成，数据调整输出中...");
            //widgetInfo->show();
        }
        else if(0x3d == redata[0]) //“=”标志，代表对方已接收到停止信号
        {
            start->setEnabled(true);
            start->highLight(true);

            isInTesting = false;
            stop_pressed = true;

            widgetInfo->hide();
            LoadConfig();
        }
        else if( 0x41 == redata[0])
        {
            QMessageBox::warning(this,tr("错误"),tr("数据超出额定，请重新输入!"),QMessageBox::tr("确定"));
            isInTesting = false;
            start->setEnabled(true);
            start->highLight(true);
        }
        else if( 0x24 == redata[0])
        {
            isInTesting = false;
            sendBuf.clear();
            redata.clear();

            qDebug()<<"hardware receive error! resend auto!";

            sendData();
        }
        else if( 0x42  == redata[0] )
        {
            QMessageBox::warning(this,tr("错误"),tr("请检查接线是否正确!"),QMessageBox::tr("确定"));
            isInTesting = false;
            start->setEnabled(true);
            start->highLight(true);
        }
        else
        {
            //qDebug()<<"redata.length()::"<<redata.length();
            array = redata.left(redata.length()-1);                 //去除停止位
            strlist = array.split('/');                             //三个数据的分隔符
            qDebug()<<"strlist.count:" <<strlist.count();
            for(i=0;i<strlist.count();i++)
            {
                m.append(strlist.at(i));                            //取到第i个位的分隔符
                //qDebug()<<"mmmmm:"<<m.toHex();
                if(i == 0) bfb_array = m;
                if(i == 1) bc_array = m;
                if(i == 2) jc_array = m;
                m.clear();
            }
            //qDebug()<<"bfb"<<bfb_array.toHex();
           // qDebug()<<"jc"<<bc_array.toHex();
           // qDebug()<<"bc"<<jc_array.toHex();

            bfb = bfb_array.toFloat();
            if(0 < bfb && bfb < 9.0)
                bfbEdit->setText(QString::number(bfb,'f',5));
            else if(9.0 <= bfb && bfb <99.0)
                bfbEdit->setText(QString::number(bfb,'f',4));
            else if(99.0 <= bfb)
                bfbEdit->setText(QString::number(bfb,'f',3));
//            showbfbEdit->setText(show_bfb);
            show_bc.append(bc_array);
            show_jc.append(jc_array);
            qDebug()<<"debug_press state"<<debug_press;
            if( debug_press == true )
            {
                qDebug()<<"debug_press is now ";

                labReternedBfb->setText(QString::number(bfb,'f',5));
                labReternedBc->setText(show_bc);
                labReternedJc->setText(show_jc);
                //bcEdit->setText(show_bc);
                //jcEdit->setText(show_jc);
            }

            show_bfb.clear();
            bfb_array.clear();
            bc_array.clear();
            jc_array.clear();
        }
        qDebug()<<bfb_array.toHex();
        bfb_array.clear();
//        statelabel->hide();
    }
}

void PAGEHUMITURE::readData1()
{
//    QByteArray data=serial1->readAll();
//    static QByteArray data_recvd;
//    QByteArray stop_data;
    //unsigned char temp;

//    stop_data.resize(1);
//    stop_data[0]=0xa5;
//    if( 0xa5 == data[0])
//    {
//        serial->write(stop_data);
//        data_recvd.clear();
//        return;
//    }
//    else
//    {
//        data_recvd.append(data);
//        if(data_recvd.length()<28)
//        {
//            return;
//        }
//        qDebug()<<"data_recvd:"<<data_recvd.toHex();

//    }
//    if( 0xaa = data_recvd[27])
//    {
//        if( 0x00 == data_recvd[0] )
//        {
//            //vEdit->setText();
//        }
//    }

}

void PAGEHUMITURE::voice(int count)//蜂鸣器多声
{
#ifndef i386
    int fd ;
    int i=0;
    fd = ::open("/dev/pwm", 'r');
    if(count == 0)
    {
        while( i<3)
        {
            ::ioctl(fd, 1, 1000);
            ::usleep(80000);
            ::ioctl(fd,0);
            i++;
            ::usleep(80000);
        }
    }
    else if(count == 1)
    {
        ::ioctl(fd,1,1000);
        ::usleep(80000);
        ::ioctl(fd,0);
    }
    ::close(fd);
#else
    qDebug()<<"beer";
#endif
}

void PAGEHUMITURE::SaveConfig() //保存设置过的textedit数据
{
    QSettings *cfg=new QSettings(":/config.ini");

    cfg->setValue("bc",bcEdit->text());
    cfg->setValue("jc",jcEdit->text());
    cfg->setValue("bfb",bfbEdit->text());
    cfg->setValue("v",vEdit->text());

    delete cfg;
}
void PAGEHUMITURE::LoadConfig() //加载设置过的textedit数据
{
    QSettings *cfg=new QSettings(":/config.ini");

    bcEdit->setText(cfg->value("bc").toString());
    jcEdit->setText(cfg->value("jc").toString());
    bfbEdit->setText(cfg->value("bfb").toString());
    vEdit->setText(cfg->value("v").toString());

    delete cfg;
}
